package com.theme.common.frame.exception;


import java.util.HashMap;
import java.util.Map;

public enum ErrorCode {

    /**
     * 400
     */
    BAD_PARAM_EXCEPTION(400, "请求参数不合法"),

    /**
     * 404
     */
    SOURCE_NOT_FOUND_EXCEPTION(404, "资源不存在"),

    /**
     * 请求方法不支持
     * */
    REQUEST_METHOD_NOT_SUPPORT(405, "请求方法不支持"),

    /**
     * 服务器内部异常
     */
    SERVER_INTERNAL_EXCEPTION(500, "服务器内部异常"),

    /******************************************* 数据操作错误异常 1001 ~ 1099 ******************************************/
    INSERT_NOT_NULL(1001,"插入对象为空"),
    BATCH_INSERT_NOT_NULL(1002,"批量插入集合为空"),
    UPDATE_NOT_NULL(1003,"更新对象为空"),
    BATCH_UPDATE_NOT_NULL(1004,"批量更新集合为空"),
    DELETE_NOT_NULL(1005,"删除id为空"),
    GET_ID_NOT_NULL(1006,"查询数据id为空"),
    GET_PARAM_NOT_NULL(1007,"条件查询参数为空"),

    /******************************************* 文件上传错误  2001 ~ 2099 ******************************************/
    FILE_UPLOAD_NULL(2001,"上传文件为空"),
    FILE_UPLOAD_FORMAT_ERROR(2002,"文件格式不支持"),
    FILE_UPLOAD_SIZE_ERROR(2003,"上传文件过大"),
    MESSAGE_PARAM_NULL(2004,"发送系统消息参数为空"),


    /******************************************* 用户 100101 ~ 100199 ******************************************/
    /**************** 表单 ******************/
    FORM_USER_USERNAME_MISSING(3000, "用户名未输入"),
    FORM_USER_PASSWORD_MISSING(3001, "密码未输入"),
    FORM_USER_CODE_MISSING(3002, "验证码未输入"),

    FORM_USER_USERNAME_ERROR(3003, "用户名错误"),
    FORM_USER_PASSWORD_ERROR(3004, "密码错误"),
    FORM_USER_USERNAME_OR_PASSWORD_ERROR(3005, "用户名或密码错误"),
    FORM_USER_CODE_ERROR(3006, "验证码错误"),

    CHECK_USER_NOT_LOGIN(3007, "用户未登录"),
    CHECK_USER_NO_AUTHORITY(3008, "无权限"),

    FORM_USER_USERNAME_ALREADY_EXIST(3009, "用户名已存在") ,
    REGISTER_USER_PHONE_MISSING(3010, "手机号未输入"),
    REGISTER_USER_PHONE_EXIST(3011, "手机号已注册"),
    REGISTER_USER_PHONE_SEND_ERROR(3012, "手机号验证码输入错误次数过多"),
    REGISTER_USER_PHONE_MAX_COUNT_ERROR(3013, "手机号发送验证码次数过多"),
    REGISTER_PHONE_SEND_RATE_ERROR (3014,"手机号验证码发送频率过快！"),
    SEND_MESSAGE_ERROR(3015,"短信发送失败！"),
    BIND_MOBILE_BIND_APPLE_EXIST(3016, "apple账号已被用户已注册或绑定"),
    BIND_APPLE_BIND_MOBILE_EXIST(3017, "手机账号已被已注册或绑定"),
    BIND_APPLE_BIND_MOBILE_USER_EXIST(3018, "该账号下存在绑定账号,请先解除绑定!"),

    EMIL_MISSING(3020,"邮箱账号未输入"),
    EMIL_FORMAT_ERROR(3021,"邮箱账号格式错误"),
    EMIL_EXIST_ERROR(3022,"邮箱账号已绑定"),
    EMIL_SEND_CODE_ERROR(3023,"邮箱发送邮件错误"),
    EMAIL_BIND_ERROR(3024,"绑定邮箱错误"),
    EMAIL_BIND_CODE_TIMEOUT(3025,"绑定邮箱信息已过期,请重新发送!"),


    /******************************************* 业务相关 ******************************************/
    USER_ALREADY_PURCHASE_THEME(4000,"用户已购买过该主题了!"),
    USER_ALREADY_DOANLOAD_THEME(4001,"用户已下载过该主题了!"),
    USER_ALREADY_BROWSE_THEME(4002,"用户已浏览过该主题了!"),
    USER_ALREADY_LIKE_THEME(4003,"用户已浏览过该主题了!"),
    USER_ALREADY_SEARCH_KEYWORD(4004,"用户已搜索过该关键词了!"),
    USER_ALREADY_FOLLOW_USER(4005,"用户已关注该用户了!"),
    USER_DOWNLOAD_THEME_EXIST(4006,"该主题不存在!"),
    USER_SIGN_IN_EXIST(4007,"本月您已经签到获取过一次VIP了,当月只有一次机会哦!"),

    USER_SUPPLEMENTARY_EXIST(4008,"本周您已经补签过一次了,一周只有一次补签机会哦!"),

    USER_ALREADY_BUY_ELEMENT_EXIST(4009,"您已经购买过了！"),

    ;


    private Integer code;

    private String msg;

    private static final Object lock = new Object() ;
    private static Map<String,ErrorCode> errorCodeMap ;
    private static Map<Integer,ErrorCode> intErrorCodeMap ;

    ErrorCode(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static ErrorCode getErrorCodeByName(String name) {
        if(errorCodeMap == null){
            synchronized (lock){
                if(errorCodeMap == null){
                    errorCodeMap = new HashMap<>() ;
                    for (ErrorCode errorCode : values()) {
                        errorCodeMap.put(errorCode.name(),errorCode) ;
                    }
                }
            }
        }
        return errorCodeMap.get(name);
    }

    public static ErrorCode getErrorCodeByCode(int code) {
        if(intErrorCodeMap == null){
            synchronized (lock){
                if(intErrorCodeMap == null){
                    intErrorCodeMap = new HashMap<>() ;
                    for (ErrorCode errorCode : values()) {
                        intErrorCodeMap.put(errorCode.code,errorCode) ;
                    }
                }
            }
        }
        return intErrorCodeMap.get(code);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }


}
